﻿using Unity.Burst;
using Unity.Mathematics;

namespace Game.FlowField.Burst
{
    /// <summary>
    /// X字位置坐标信息，即某个点左上、右上、左下、右下
    /// </summary>
    [BurstCompile]
    public struct XQuadPoint
    {
        public readonly int2 MapSize;
        /// <summary>
        /// 是否存在左上格子
        /// </summary>
        public bool ContainsIndexA;
        /// <summary>
        /// 左上格子索引
        /// </summary>
        public int IndexA;
        /// <summary>
        /// 左上格子2D索引
        /// </summary>
        public int2 Index2DA;
        /// <summary>
        /// 是否存在右上格子
        /// </summary>
        public bool ContainsIndexB;
        /// <summary>
        /// 右上格子索引
        /// </summary>
        public int IndexB;
        /// <summary>
        /// 右上格子2D索引
        /// </summary>
        public int2 Index2DB;
        /// <summary>
        /// 是否存在左下格子
        /// </summary>
        public bool ContainsIndexC;
        /// <summary>
        /// 左下格子索引
        /// </summary>
        public int IndexC;
        /// <summary>
        /// 左下格子2D索引
        /// </summary>
        public int2 Index2DC;
        /// <summary>
        /// 是否存在右下格子
        /// </summary>
        public bool ContainsIndexD;
        /// <summary>
        /// 右下格子索引
        /// </summary>
        public int IndexD;
        /// <summary>
        /// 右下格子2D索引
        /// </summary>
        public int2 Index2DD;

        public XQuadPoint(int2 mapSize)
        {
            this.MapSize = mapSize;
            this.ContainsIndexA = default;
            this.IndexA = default;
            this.Index2DA = default;
            this.ContainsIndexB = default;
            this.IndexB = default;
            this.Index2DB = default;
            this.ContainsIndexC = default;
            this.IndexC = default;
            this.Index2DC = default;
            this.ContainsIndexD = default;
            this.IndexD = default;
            this.Index2DD = default;
        }

        /// <summary>
        /// 传入的<paramref name="center"/>和<paramref name="index"/>必须是正确的存在的点否则结果也不对
        /// </summary>
        /// <param name="center">该点的2D索引</param>
        /// <param name="index">该点的索引</param>
        [BurstCompile]
        public void Override(int2 center, int index)
        {
            this.Index2DA = new int2(center.x - 1, center.y + 1);
            this.Index2DB = new int2(center.x + 1, center.y + 1);
            this.Index2DC = new int2(center.x - 1, center.y - 1);
            this.Index2DD = new int2(center.x + 1, center.y - 1);

            this.IndexA = index - this.MapSize.y + 1;
            this.IndexB = index + this.MapSize.y + 1;
            this.IndexC = index - this.MapSize.y - 1;
            this.IndexD = index + this.MapSize.y - 1;

            this.ContainsIndexA = this.Index2DA.x >= 0 && this.Index2DA.y < this.MapSize.y;
            this.ContainsIndexB = this.Index2DB.x < this.MapSize.x && this.Index2DB.y < this.MapSize.y;
            this.ContainsIndexC = this.Index2DC.x >= 0 && this.Index2DC.y >= 0;
            this.ContainsIndexD = this.Index2DD.x < this.MapSize.x && this.Index2DD.y >= 0;
        }
    }
}